<template>
  <div class="container full-screen flex">
    <el-card class="card" shadow="hover">
      <div class="head primary text-center">
        <h2>用户认证</h2>
      </div>
      <el-form
        :model="user"
        ref="user"
        :rules="user_rule"
        style="margin-top: 20px"
      >
        <el-form-item prop="name">
          <el-input
            v-model="user.name"
            placeholder="姓名"
            prefix-icon="iconfont iconuser"
          ></el-input>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input
            v-model="user.mobile"
            :disabled="change"
            placeholder="手机号"
            prefix-icon="iconfont iconuser"
          ></el-input>
        </el-form-item>
        <el-form-item prop="idNumber">
          <el-input
            v-model="user.idNumber"
            :disabled="change"
            prefix-icon="iconfont iconid-card-o"
            placeholder="身份证号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="address">
          <el-input
            v-model="user.address"
            placeholder="地址"
            prefix-icon="iconfont iconaddress2"
          ></el-input>
        </el-form-item>
        <el-form-item prop="sex">
          <el-select
            v-model="user.sex"
            placeholder="请选择性别"
            style="width: 100%"
          >
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="age">
          <el-input
            v-model="user.age"
            placeholder="年龄"
            prefix-icon="iconfont icontel2"
          ></el-input>
        </el-form-item>

        <el-form-item prop="description">
          <el-input
            v-model="user.description"
            placeholder="描述"
            prefix-icon="iconfont iconpeople1"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button
        type="primary"
        :loading="registerLoading"
        @click="onSubmit"
        style="width: 100%"
        >提交
      </el-button>
    </el-card>
  </div>
</template>

<script>
import { checkIdNumber, checkTel } from "/src/utils/validator.js";
import { authenticate } from "@/network/account";
import { getInfo,changeInfo } from "@/network/user.js";

export default {
  name: "Auth",
  components: {},
  data() {
    return {
      identity: this.$route.params.identity,
      change: false,
      user: {
        name: "",
        sex: "",
        idNumber: "",
        address: "",
        age: "",
        description: "",
        mobile: "",
      },
      user_rule: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        mobile:[{ required: true, message: "请输入手机号", trigger: "blur" }],
        // mobile: [{ required: true, validator: checkTel, trigger: "blur" }],
        idNumber: [
          { required: true, validator: checkIdNumber, trigger: "blur" },
        ],
        address: [{ required: true, message: "请输入住址", trigger: "blur" }],
      },
      registerLoading: false,
    };
  },
  created() {
    this.registerLoading = true
    if (window.localStorage.getItem("authorization") != null) {
      this.change = true;
      console.log(this.change);
      getInfo()
        .then((res) => {
          Object.assign(res.data, res.data2);
          this.user = res.data;
          // this.$store.commit('setEmployee', data)
        })
        .then(() => {
          // this.employee = this.$store.getters.Employee
        })
        .catch((err) => {
          this.$message.error("网络错误");
        });
    }
    this.registerLoading = false
  },
  methods: {
    onSubmit() {
      this.$refs.user.validate((valid) => {
        if (valid) {
          this.submit(this.user);
        }
      });
    },
    submit(data) {
      this.registerLoading = true;
      if (this.change == true) {
        changeInfo(data).then(res=>{
          if(res.code==200){
            this.$message.success("修改成功");
            this.$router.replace({
              name:"UserInfo"
            })
          } else{
            this.$message.error(res.data)
          }        
        },err=>{})
      } else {
        authenticate(data).then((res) => {
          if (res.code == 200) {
            this.registerLoading = false;
            this.$message.success("认证成功");
            this.$router.replace({
              name: "Home",
            });
          }
        });
      }
    },
  },
};
</script>

<style scoped>
.container {
  background: url(/img/bg.jpg);
  background-size: cover;
  flex-direction: column;
  align-items: center;
}

.head {
  letter-spacing: 4px;
}

.card {
  width: 450px;
  margin: auto;
}
</style>
